package com.linkq.ipinyin.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.linkq.core.base.BaseService;
import com.linkq.ipinyin.dao.CnCharacterDao;
import com.linkq.ipinyin.mapper.CnCharacterMapper;
import com.linkq.ipinyin.mapper.TextMapper;
import com.linkq.ipinyin.model.CnCharacter;
import com.linkq.ipinyin.model.Text;
import com.linkq.ipinyin.vo.ResultInfo;
import com.xiaoleilu.hutool.bean.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @FileName BcCnCharacterService
 * @Description
 * @Author wangpeng
 * @date 2024-11-13
 **/

@Service
@Transactional
public class CnCharacterService extends BaseService<CnCharacterDao, CnCharacter> {
    @Autowired
    private CnCharacterMapper cnCharacterMapper;
    @Autowired
    private TextMapper textMapper;
    public ResultInfo add(CnCharacter cnCharacter) {
        int row = cnCharacterMapper.insert(cnCharacter);
        if (row > 0) {
            return ResultInfo.success(row);
        }
        return ResultInfo.error("新增汉字失败", row);
    }

    public List<CnCharacter> getCharacterList(CnCharacter cnCharacter) {
        List<Integer> textIds = new ArrayList<>();
        List<Text> texts = new ArrayList<>();
        if (!Objects.isNull(cnCharacter.getGrade())) {
            texts = textMapper.selectList(
                    Wrappers.<Text>lambdaQuery()
                            .eq(Text::getGrade, cnCharacter.getGrade())
            );
            if (CollectionUtils.isNotEmpty(texts)) {
                textIds = texts.stream().map(Text::getId).collect(Collectors.toList());
            }
        }
        List<CnCharacter> list = cnCharacterMapper.selectList(
                Wrappers.<CnCharacter>lambdaQuery()
                        .eq(StringUtils.isNotBlank(cnCharacter.getChCharacter()), CnCharacter::getChCharacter, cnCharacter.getChCharacter())
                        .eq(StringUtils.isNotBlank(cnCharacter.getVowel()), CnCharacter::getVowel, cnCharacter.getVowel())
                        .eq(StringUtils.isNotBlank(cnCharacter.getInitial()), CnCharacter::getInitial, cnCharacter.getInitial())
                        .eq(StringUtils.isNotBlank(cnCharacter.getPinyin()), CnCharacter::getPinyin, cnCharacter.getPinyin())
                        .eq(!Objects.isNull(cnCharacter.getCharacterType()), CnCharacter::getCharacterType, cnCharacter.getCharacterType())
                        .eq(!Objects.isNull(cnCharacter.getTone()), CnCharacter::getTone, cnCharacter.getTone())
                        .eq(!Objects.isNull(cnCharacter.getBelongText()), CnCharacter::getBelongText, cnCharacter.getBelongText())
                        .eq(CollectionUtils.isNotEmpty(textIds), CnCharacter::getBelongText, textIds)
        );
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(x -> {
                Integer textId = x.getBelongText();
                Text text = textMapper.selectById(textId);
                x.setGrade(Objects.isNull(text) ? null : text.getGrade());
                x.setTextName(Objects.isNull(text) ? null : text.getTitle());
            });
        }
        return list;
    }

    public ResultInfo delete(List<Integer> ids) {
        int row = cnCharacterMapper.deleteBatchIds(ids);
        if (row > 0) {
            return ResultInfo.success(row);
        }
        return ResultInfo.error("删除失败", row);
    }

    public ResultInfo edit(CnCharacter cnCharacter) {
        CnCharacter origin = cnCharacterMapper.selectById(cnCharacter.getId());
        if (Objects.isNull(origin)) {
            return ResultInfo.error("汉字不存在", -1);
        }
        BeanUtil.copyProperties(cnCharacter, origin);
        int row = cnCharacterMapper.updateById(origin);
        if (row > 0) {
            return ResultInfo.success(row);
        }
        return ResultInfo.error("编辑失败", row);
    }
}
